$(function(){

  // 判断页面是否有地址信息，有则把信息填写到对应位置
  if(localStorage.getItem('addressInfo')){
    var _addressInfo = JSON.parse(localStorage.getItem('addressInfo'));
    $('#city').text(_addressInfo.city);
    $('#area-result').text(_addressInfo.area).removeClass('no-result');
    $('#community-result').val(_addressInfo.community);
    $('#address-detail').val(_addressInfo.addressDetail);
  }

  // 选择区域
  $("#areaList").on('click','li',function(){
    $('#area-result').text($(this).text()).removeClass("no-result");
    loadCommunity($(this).text());
  });

  // 选择小区
  $("#communityList").on('click','li',function(){
    $('#community-result').text($(this).text()).removeClass("no-result");
  });

  $(".add_address--btn").click(function(){
    var _city = $('#city').text(), _areaTxt, _communityTxt;
    if($('#area-result').hasClass('no-result')){
      alert("请选择区域");
      return false;

    }else{
      _areaTxt = $('#area-result').text();
    }

    if($('#community-result').val()==""){
      alert("请选择小区");
      return false;
    }else{
      _communityTxt = $("#community-result").val();
    }
    var _json = {
      'city': _city,
      'area': _areaTxt,
      'community': _communityTxt,
      'addressDetail': $('#address-detail').val()
    }
    localStorage.setItem('addressInfo',JSON.stringify(_json));
    var address = $('#address-detail').val();
    location.href = 'orderConfirm.html?city='+_city+'&area='+_areaTxt+'&community='+_communityTxt+'&address='+address+'&serviceId='+serviceId+"&chargesType="+chargesType+"&months="+months+"&price="+price;
  });

});